﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using SevenShine;
using DG.Tweening;
using UnityEngine.SceneManagement;
using System;

public class IntroduceControl : MonoBehaviour {

    private Image blackBG;
    private Text text;
    private AudioSource audios;

    //public AudioClip TypeWriting;

    public static IntroduceControl Instance;

	// Use this for initialization
	void Start ()
    {
        Instance = this;
        audios = GameObject.Find("Main Camera").GetComponent<AudioSource>();

        Instantiate(Resources.Load("Prefabs/UI/Narratage"), gameObject.transform);
        blackBG = GameObject.Find("BlackBG").GetComponent<Image>();
        text = GameObject.Find("XuText").GetComponent<Text>();

        if (SceneManager.GetActiveScene().name=="001")
        {
            Debug.Log("序");
            Introduce1();
        }
        else if (SceneManager.GetActiveScene().name=="002")
        {
            Introduce2();
        }
        else if (SceneManager.GetActiveScene().name == "003")
        {
            Introduce3();
        }
    }



    private void Introduce1()
    {
        //各个携程的嵌套可以更换
        StartCoroutine(Narratage(() =>
        {
            StartCoroutine(Introduce1_1(() =>
            {
                StartCoroutine(LastClear(() =>
                {
                    StartCoroutine(PlayVideo1(() =>
                    {
                        Debug.Log("Animation Finish!");
                    }));
                }));
            }));
        }));

    }

    IEnumerator PlayVideo1(Action action=null)
    {
        Animator ani=GameObject.Find("Weather").GetComponent<Animator>();
        ani.SetTrigger("weather1");

        //更换龙卷风图标
        yield return new WaitForSeconds(10);
        DragUI.Instance.storm.gameObject.SetActive(false);
        DragUI.Instance.StormReplace.SetActive(true);

        if (action != null)
        {
            action();
        }
        
    }

    void Introduce2()
    {
        //各个携程的嵌套可以更换
        StartCoroutine(Introduce2_1(() =>
        {
            StartCoroutine(LastClear(() =>
            {
                StartCoroutine(Level2Dialog(() =>
                {
                    Debug.Log("Animation Finish!");
                }));
            }));
        }));
       
    }

    void Introduce3()
    {
        //各个携程的嵌套可以更换
        StartCoroutine(Introduce3_1(() =>
        {
            StartCoroutine(LastClear(() =>
            {
                StartCoroutine(Level3Begin(() =>
                {
                    Debug.Log("Animation Finish!");
                }));
            }));
        }));
    }

    void Level2Last()
    {
        //各个携程的嵌套可以更换
        StartCoroutine(WalkToPyramid(() =>
        {   
        }));
    }

    public void Level2LastFade()
    {
        StartCoroutine(LastAppear(()=>{ }));
    }

    public void Level2LastIntroduce()
    {
        StartCoroutine(Level2IntroduceLast(()=> { }));
    }

    IEnumerator Level2IntroduceLast(Action action = null)
    {
        yield return new WaitForSeconds(0);
        //text.text = "";

        //audios.clip = TypeWriting;
        //audios.Play();
        //audios.loop = true;

        //text.DOText(LanguageMgr.GetInstance().ShowText("Level2_Panbai2"), 2).SetEase(Ease.Linear).OnComplete(() =>
        //{
        //    //text.text = "";
        //    audios.Stop();
        //    SceneManager.LoadScene("003");
        //    //在Dotween函数中嵌套自己的回调
        //    if (action != null)
        //    {
        //        action();
        //    }
        //});
        text.text = LanguageMgr.GetInstance().ShowText("Level2_Panbai2");
        yield return new WaitForSeconds(2);
        //下一关
        SceneManager.LoadScene("003");
        if (action != null)
        {
            action();
        }
    }

    IEnumerator WalkToPyramid(Action action=null)
    {
        yield return new WaitForSeconds(0);
        PlayerLevel2.Instance.ToDestination();
        if (action != null)
        {
            action();
        }
    }

    IEnumerator Level3Begin(Action action=null)
    {
        yield return new WaitForSeconds(0);
        //开始移动
        GameObject.Find("Role_XiaoQi").SendMessage("BeginMove");
        if (action != null)
        {
            action();
        }
    }

    IEnumerator Introduce3_1(Action action=null)
    {
        yield return new WaitForSeconds(1);
        //text.text = "";

        //audios.clip = TypeWriting;
        //audios.Play();
        //audios.loop = true;

        //text.DOText(LanguageMgr.GetInstance().ShowText("Level3_Panbai"), 2).SetEase(Ease.Linear).OnComplete(() =>
        //{
        //    text.text = "";
        //    audios.Stop();
        //    //在Dotween函数中嵌套自己的回调
        //    if (action != null)
        //    {
        //        action();
        //    }
        //});

        text.text = LanguageMgr.GetInstance().ShowText("Level3_Panbai");
        if (action != null)
        {
            action();
        }
    }

    IEnumerator Level2Dialog(Action action = null)
    {
        yield return new WaitForSeconds(0);
        
        GameObject.Find("Role_XiaoQi").SendMessage("ToTarget2");
    }

    IEnumerator Introduce2_1(Action action = null)
    {
        yield return new WaitForSeconds(1);
        //text.text = "";

        //audios.clip = TypeWriting;
        //audios.Play();
        //audios.loop = true;

        //text.DOText(LanguageMgr.GetInstance().ShowText("Level2_Panbai1"), 4).SetEase(Ease.Linear).OnComplete(() =>
        //{
        //    text.text = "";
        //    audios.Stop();
        //    //在Dotween函数中嵌套自己的回调
        //    if (action != null)
        //    {
        //        action();
        //    }
        //});

        text.text = LanguageMgr.GetInstance().ShowText("Level2_Panbai1-1");
        yield return new WaitForSeconds(2);
        text.text = LanguageMgr.GetInstance().ShowText("Level2_Panbai1-2");
        yield return new WaitForSeconds(2);
        text.text = LanguageMgr.GetInstance().ShowText("Level2_Panbai1-3");
        yield return new WaitForSeconds(2);
        if (action != null)
        {
            action();
        }
    }

    IEnumerator Narratage(Action action=null)
    {
        yield return new WaitForSeconds(1);

        //audios.clip = TypeWriting;
        //audios.Play();
        //audios.loop = true;

        //text.text = "";
        //text.DOText(LanguageMgr.GetInstance().ShowText("Xu_Introduce1"),3).SetEase(Ease.Linear).OnComplete(()=>
        //{
        //    //audios.Stop();

        //    //在Dotween函数中嵌套自己的回调
        //    if (action!=null)
        //    {
        //        action();
        //    }
        //});

        text.text = LanguageMgr.GetInstance().ShowText("Xu_Introduce1");
        yield return new WaitForSeconds(2);
        text.text = LanguageMgr.GetInstance().ShowText("Xu_Introduce2");
        yield return new WaitForSeconds(2);
        text.text = LanguageMgr.GetInstance().ShowText("Xu_Introduce3");
        if (action != null)
        {
            action();
        }
    }

    IEnumerator Introduce1_1(Action action=null)
    {
        yield return new WaitForSeconds(2);
        //text.text = "";

        //audios.clip = TypeWriting;
        //audios.Play();
        //audios.loop = true;

        //text.DOText(LanguageMgr.GetInstance().ShowText("Level1_Panbai1"), 4).SetEase(Ease.Linear).OnComplete(() =>
        //{
        //    text.text = "";
        //    audios.Stop();
        //    if (action != null)
        //    {
        //        action();
        //    }
        //});

        text.text = LanguageMgr.GetInstance().ShowText("Level1_Panbai1-1");
        yield return new WaitForSeconds(2);
        text.text = LanguageMgr.GetInstance().ShowText("Level1_Panbai1-2");
        yield return new WaitForSeconds(2);
        text.text = LanguageMgr.GetInstance().ShowText("Level1_Panbai1-3");
        if (action != null)
        {
            action();
        }
    }

    IEnumerator Introduce1_2(Action action=null)
    {
        yield return new WaitForSeconds(0);

        //audios.clip = TypeWriting;
        //audios.Play();
        //audios.loop = true;

        //text.DOText(LanguageMgr.GetInstance().ShowText("Level1_Panbai2"), 2).SetEase(Ease.Linear).OnComplete(()=>
        //{
        //    audios.Stop();
        //    if (action != null)
        //    {
        //        action();
        //    }
        //});
        text.text = LanguageMgr.GetInstance().ShowText("Level1_Panbai2");
        yield return new WaitForSeconds(2);
        if (action != null)
        {
            action();
        }
    }

    IEnumerator NextLevel2(Action action = null)
    {
        yield return new WaitForSeconds(0);
        SceneManager.LoadScene("002");
        if (action != null)
        {
            action();
        }
    }

    void Introduce1Last()
    {
        //各个携程的嵌套可以更换
        StartCoroutine(LastAppear(() =>
        {
            StartCoroutine(Introduce1_2(() =>
            {
                StartCoroutine(NextLevel2(() =>
                {
                    Debug.Log("Animation Finish!");
                }));
            }));
        }));
    }



    //渐隐
    IEnumerator LastClear(Action action=null)
    {
        yield return new WaitForSeconds(2);
        text.text = "";
        blackBG.DOFade(0,2).SetEase(Ease.Linear).OnComplete(() =>
        {
            blackBG.gameObject.SetActive(false);

            if (action != null)
            {
                action();
            }
        });
        
    }
    //渐现
    IEnumerator LastAppear(Action action = null)
    {
        yield return new WaitForSeconds(0);
        blackBG.gameObject.SetActive(true);
        text.text = "";
        blackBG.DOFade(1,2).OnComplete(()=>
        {
            if (action != null)
            {
                action();
            }
        });
        
    }

}
